package com.module.admin.sys.utils;

import java.util.List;

import com.module.admin.sys.pojo.SysMenu;
import com.module.admin.sys.pojo.SysUser;
import com.module.admin.sys.service.SysUserService;
import com.system.comm.utils.FrameMd5Util;
import com.system.comm.utils.FrameSpringBeanUtil;

/**
 * 用户的常用工具类
 * @author yuejing
 * @date 2016年5月6日 下午2:41:47 
 * @version V1.0
 */
public class SysUserUtil {
	
	/**
	 * 对用户的密码进行加密<br/>
	 * @param id
	 * @param password
	 * @return
	 */
	public static String encryptionPassword(String password) {
		password = FrameMd5Util.getInstance().encodePassword(password, "admin");
		password = FrameMd5Util.getInstance().encodePassword(password);
		return password;
	}

	public static SysUser get(Integer userId) {
		SysUserService sysUserService = FrameSpringBeanUtil.getBean(SysUserService.class);
		return sysUserService.get(userId);
	}
	public static String getUserName(Integer userId) {
		SysUser user = get(userId);
		return user == null ? null : user.getUsername();
	}
	public static String getNickname(Integer userId) {
		SysUser user = get(userId);
		return user == null ? null : user.getNickname();
	}
	/**
	 * 判断用户是否存在对应的权限
	 * @param user
	 * @param mid
	 * @return
	 */
	public static boolean isAuth(SysUser user, String mid) {
		List<SysMenu> menus = user.getMenus();
		if (menus != null) {
			for (SysMenu m : menus) {
				if (m.getMenuId().equals(mid)) {
					return true;
				}
				if (m.getCldMenus() != null) {
					for (SysMenu m1 : m.getCldMenus()) {
						if(m1.getMenuId().equals(mid)){
							return true;
						}
						if (m1.getCldMenus() != null) {
							for (SysMenu m2 : m1.getCldMenus()) {
								if(m2.getMenuId().equals(mid)){
									return true;
								}
							}
						}
					}
				}
			}
		}
		return false;
	}
	
	/*public static void main(String[] args) {
		System.out.println(encryptionPassword("123456"));
	}*/
}
